package vo;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class RegisterDAO {
	public boolean register(Connection con, User user) throws Exception {
		boolean flag = false;
		PreparedStatement pstmt = null,pstmtt = null;
		String sql = "INSERT INTO user(realname,username,userpwd) VALUES(?,?,?)";
		pstmtt = con.prepareStatement("select * from user where username=?");
		pstmtt.setString(1,user.getXm());
		ResultSet rs = pstmtt.executeQuery();
		if(rs.next()) {
			con.close();
			return false;
		}
		try {
			pstmt = con.prepareStatement(sql);
			pstmt.setString(1, user.getRealxm());
			pstmt.setString(2, user.getXm());
			pstmt.setString(3, user.getMm());
			flag = (pstmt.executeUpdate()==1);
		} catch (SQLException e) {
			if(!e.getMessage().contains("PRIMARY")){
				e.printStackTrace();
		}
		}finally {
			try {
				if(pstmt!=null)pstmt.close();
				if(con!=null)con.close();
			}catch (SQLException e) {	
				e.printStackTrace();
			}
		}
		return flag;
}
}
